use leptos::{context::Provider, prelude::*};
use thaw_utils::{BoxOneCallback, Model};

#[component]
pub fn RadioGroup(
    #[prop(into)] name: String,
    #[prop(optional, into)] value: Model<Option<String>>,
    #[prop(optional, into)] on_change:  Option<BoxOneCallback<Option<String>>>,
    children: Children,
) -> impl IntoView {
    Effect::new(move |_|{
        if let Some(on_change) = on_change.as_ref() {
            on_change(value.get());
        }
    });
    view! {
        <div class="flex flex-row gap-2">
            <Provider value=RadioGroupInjection{value,name}>
                {children()}
            </Provider>
        </div>
     }
}

#[derive(Clone)]
pub(crate) struct RadioGroupInjection{
    pub name: String,
    pub value:Model<Option<String>>
}

pub(crate) fn use_radio_group() -> RadioGroupInjection {
    expect_context()
}
